package com.example.service;

import com.example.mapper.MobileMapper;
import com.example.pojo.PolicyVo;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.pojo.mobile.MobileMedicalPolicy;
import com.example.pojo.mobile.PersonalVo;
import com.example.pojo.mobile.UserInfo;
import com.example.utils.JwtUtils;
import com.example.utils.MD5Utils;
import com.example.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class MobileService {
    @Autowired
    MobileMapper mobileMapper;
    public Result register(User user) {
        /**
         * 非空校验
         */
        /**
         * 非空校验
         */
        if(!StringUtils.hasText(user.getUname())){
            return new Result(100,"用户名不能为空",null);
        }
        if(!StringUtils.hasText(user.getPwd())){
            return new Result(100,"密码不能为空",null);
        }
        if(!StringUtils.hasText(user.getPhonenumber())){
            return new Result(100,"电话号码不能为空",null);
        }
        if(!StringUtils.hasText(user.getRealname())){
            return new Result(100,"真实姓名不能为空",null);
        }

        /**
         * 是否存在
         */
        User u=mobileMapper.getUserByUname(user.getUname());
        if(u!=null){
            return new Result(100,"该用户名已存在",null);
        }
        user.setCreatetime(new Date());
        user.setUpdatetime(new Date());
        user.setUtype("ROLE_1");
        // System.err.println(user);
        //密码加密
        String md5Pwd= MD5Utils.stringToMD5(user.getPwd());
        user.setPwd(md5Pwd);

        //影响的函数
        int row = mobileMapper.addUser(user);

        if(row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"注册成功",null);
    }

    public Result login(User user) {
        if(!StringUtils.hasText(user.getUname())){
            return new Result(100,"用户名不能为空",null);
        }

        if (!StringUtils.hasText(user.getPwd())){
            return new Result(100,"密码不能为空",null);
        }
        /**
         * 查询
         */
        //MD5加密
        //
        String md5Password = MD5Utils.stringToMD5(user.getPwd());

       // System.err.println(md5Password);

        User u = mobileMapper.getUserByNameAndPwd(user.getUname(),md5Password);

        if (u==null){
            return new Result(100,"账号或者密码错误",null);
        }

        String token= JwtUtils.getJwtToken(u.getUname());
        UserInfo userInfo=new UserInfo();
        userInfo.setUsername(u.getUname());
        userInfo.setToken(token);
        userInfo.setHeadImg("");
        System.err.println("userInfo.setHeadImg(\"\")");
        PersonalVo personalVo=mobileMapper.getUserInfoBuUserId(u.getId());

        userInfo.setHeadImg(personalVo.getHeadImg());
        RedisUtil.set("mediacl:mobile:login",token,60*30);

        return new Result(100,"登录成功",userInfo);
    }

    public Result getLastPolicy() {
        List<MobileMedicalPolicy> list=mobileMapper.getLastPolicy();
        String[] iconArray={

        };
        if(list!=null && list.size()>0){
            for (MobileMedicalPolicy mobileMedicalPolicy:list){
                int index=new Random().nextInt(iconArray.length);
                mobileMedicalPolicy.setIcon(iconArray[index]);
            }
        }
        return new Result(200,"s数据加载成功",list);
    }

    public Result getPolicyList() {
        List<MobileMedicalPolicy> list=mobileMapper.getPolicyList();
        String[] iconArray={

        };
        if(list!=null && list.size()>0){
            for (MobileMedicalPolicy mobileMedicalPolicy:list){
                int index=new Random().nextInt(iconArray.length);
                mobileMedicalPolicy.setIcon(iconArray[index]);
            }
        }
        return new Result(200,"数据加载成功",list);
    }

    public Result getLoginUserInfo(String token) {
        if(!StringUtils.hasText(token)){
            return new Result(101,"token不能为空",null);
        }
        String redisToken=RedisUtil.get("mediacl:mobile:login");
        if(!StringUtils.hasText(redisToken)){
            return new Result(101,"token已过期，请重新登录",null);
        }
        String username=JwtUtils.getMemberNameByJwtToken(token);

        PersonalVo personalVo= mobileMapper.getUserByName(username);
        if(personalVo==null){
            return new Result(100,"该用户不存在",null);
        }

        PersonalVo personalVo2 =mobileMapper.getUserInfoBuUserId(personalVo.getId());
        if (personalVo2==null){
            return new Result(100,"用户不存在",null);
        }
        personalVo.setHeadImg(personalVo2.getHeadImg());
        return new Result(200,"数据加载成功",null);
    }

    public Result uploadHeadImage(MultipartFile file) {
        String fileName=file.getOriginalFilename();
        String newFileName=System.currentTimeMillis()+fileName.substring(fileName.lastIndexOf("."));
        String path="D:\\upload5";
        File f=new File(path);
        if (!f.exists()){
            f.mkdirs();
        }
        try {
            file.transferTo(new File(path+File.separator+newFileName));
            String url="http://"+newFileName;
            String redisToken= RedisUtil.get("mediacl:mobile:login");
            System.err.println(url);
            if(!StringUtils.hasText(redisToken)){
                return new Result(101,"token已过期，请重新登录",null);
            }
            String username= JwtUtils.getMemberNameByJwtToken(redisToken);
            PersonalVo personalVo= mobileMapper.getUserByName(username);
            int row=mobileMapper.updateUserInfoByUserId(personalVo.getId(),url);
            if(row!=1){
                return new Result(200,"文件上传成功",url);
            }
            return new Result(200,"文件上传成功",url);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(500,"服务器错误",null);
    }
}
